from apps import db
from datetime import datetime
import copy

class BaseModel(object):
    """模型基类"""
    create_time = db.Column( db.DateTime, default=datetime.now, comment="添加时间" )
    update_time = db.Column( db.DateTime, default=datetime.now, onupdate=datetime.now,comment="更新时间" )
    sort        = db.Column( db.Integer, default=0,comment="排序" )
    is_deleted  = db.Column( db.Boolean, default=False,comment="逻辑删除" )
    is_show     = db.Column( db.Boolean, default=True,comment="是否显示" )

    def __to_dict__(self,fields=None):
        """把模型转换成字典"""
        try:
            self.create_time = self.create_time.strftime("%Y-%m-%d %H:%M:%S")
        except:
            pass
        try:
            self.update_time = self.update_time.strftime("%Y-%m-%d %H:%M:%S")
        except:
            pass

        data = self.__dict__
        if fields is None:
            data_dict = copy.deepcopy(data)
            if "_sa_instance_state" in data_dict:
                data_dict.pop("_sa_instance_state")
        else:
            data_dict = {}
            for key,value in data.items():
                if key in fields:
                    data_dict[key] = value
        return data_dict
